package com.warehouse.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import lombok.Getter;
import lombok.Setter;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

@Getter
@Setter
public class CheckCodeAction extends ActionSupport {
    private ByteArrayInputStream inputStream;//用于输出验证码
    private static final long serialVersionUID = 1L;
    private static final int HEIGHT =30;
    private static final int WIDTH = 80;
    private StringBuffer randomCode;

    public String createCheckCode() throws IOException {
        // 内存中的一副图片,指定宽 高，类型 实际开发就用REB
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);
        // 往图片上写数据 代表图形 往图形对象上写数据，肯定要利用它的方法 很多draw
        Graphics g = image.getGraphics();
        // 1,设置背景色
        setBackGround(g);
        // 2,设置边框
        setBorder(g);
        // 3,画干扰线
        drawRandomLine(g);
        // 4,写随机数
        drawRandomNum((Graphics2D) g);
        ActionContext.getContext().getSession().put("randomCode", randomCode.toString());
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageOutputStream imageOut= ImageIO.createImageOutputStream(output); ;
        ImageIO.write(image, "jpg", imageOut);
        imageOut.close();
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        this.setInputStream(input);
        return Action.SUCCESS;
    }

    private void drawRandomNum(Graphics2D g) {
        g.setColor(Color.RED);
        g.setFont(new Font("宋体",Font.BOLD,20));
        int x=10;
        //String base = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a";
        String base = "abceefjhijklmnopqrstABCEEFJHIJKLMNOPQRST1234567890";
        //写4个汉字 [\u4e00-\u9fa5] 匹配汉字
        randomCode = new StringBuffer();
        for(int i=0;i<4;i++){
            //30*3.14/180 代表30度
            int degree = new Random().nextInt()%25;//-30到30
            String ch = base.charAt(new Random().nextInt(base.length()))+"";
            g.rotate(degree*Math.PI/180,x,20);//设置旋转弧度
            g.drawString(ch, x, 20);
            g.rotate(-degree*Math.PI/180,x,20);//不要影响下一次旋转
            x+=18;//设置相邻的字符之间的间距
            randomCode.append(ch);
        }
    }

    private void drawRandomLine(Graphics g) {
        g.setColor(Color.GREEN);
        for (int i = 0; i < 5; i++) {
            int x1 = new Random().nextInt(WIDTH);
            int y1 = new Random().nextInt(HEIGHT);
            int x2 = new Random().nextInt(WIDTH);
            int y2 = new Random().nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private void setBorder(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
    }

    private void setBackGround(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
    }
}
